const express = require("express");
const router = express.Router();
const { v1: uuidV1 } = require("uuid");
var bodyParser = require("body-parser");

// 创建application/json 解析器
var jsonParser = bodyParser.json();


var mysql = require("mysql");

var pool = mysql.createPool({
    host: '47.107.243.60',
    user: 'Flechazo',
    password: 'MySQL5.7',
    database: 'socket',
    port: 3306
});

//callback为回调方法
async function queryDataBySQL(sql, callback) {
    return await new Promise((reslove,reject)=>{
        pool.getConnection(function (err, connection) {
          connection.query(sql, function (err, result) {
              if (err) { reject(err) } else {
                if(callback){
                  callback(result)
                }
                reslove(1)
              }
  
          });
          connection.release();
        });
    })
};

// 查询是否是新聊天记录
router.get('/is_chated',function(req,res){
    const {chatId} = req.query
    queryDataBySQL(`SELECT chatId FROM personmsg WHERE chatId = '${chatId}';`,(result)=>{
        if(JSON.parse(JSON.stringify(result)).length == 1){
            res.send({code:200,msg:'成功',is_chated:true})
        }else{
            res.send({code:200,msg:'成功',is_chated:false})
        }
    })
})

module.exports = router;
